home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Frameworks
/
Grant's CGI Framework 1.0b14
/
Util
/
ErrorUtil.c
< prev
next >
Wrap
Text File
|
1996-04-10
|
6KB
|
226 lines
/*****
*
* ErrorUtil.c
*
* This is a support file for "Grant's CGI Framework".
* Please see the license agreement that accompanies the distribution package
* for licensing details.
*
* Copyright ©1995,1996 by Grant Neufeld
* grant@acm.com
* http://arpp.carleton.ca/grant/
*
*****/
#include "MyConfiguration.h"
#include <stdio.h>
#include <string.h>
#include "compiler_stuff.h"
#include "constants.h"
#include "globals.h"
#include "DebugUtil.h"
#include "ErrorInt.h"
#include "LogUtil.h"
#include "MemoryUtil.h"
#include "Quit.h"
#include "StringUtil.h"
#include "ErrorUtil.h"
/*** LOCAL CONSTANTS ***/
#define kstrErrorMissing "\pUnknown! Note the id number and contact the developer of this software."
/* the size of the above string + 1 for size byte */
#define kstrErrorMissingSize 72
/*** LOCAL PROTOTYPES ***/
static StringHandle errorSystemGetString ( OSErr, Str255 * );
static void errorSystemDisposeString ( StringHandle );
static void errorStartupGetString ( short, Str255 * );
/*** FUNCTIONS ***/
/* */
p_export
void
ErrorSystemHandler ( OSErr errNum, Boolean useAlert )
{
#if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
StringHandle errStrHdl; /* the human readable error string */
Str255 errNameStr; /* the name of the error */
#endif
#if kCompileWithErrorLogging
Str255 errNumStr; /* used for string version of the error number */
#endif
/* Create a pascal format string of the error number. */
#if kCompileWithErrorLogging
NumToString ( errNum, errNumStr );
LogStringAndSeparatorP ( errNumStr, '\t' );
#endif /* kCompileWithErrorLogging */
#if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
errStrHdl = errorSystemGetString ( errNum, &errNameStr );
if ( (errStrHdl != NULL) && (*errStrHdl != NULL) )
{
/* errStrHdl was successfully retreived */
#if kCompileWithForeground && kCompileWithFullUserInterface
if ( useAlert )
{
/* Display the alert. */
ErrorAlertSystemString ( errNum, errStrHdl, &errNameStr );
}
#endif
#if kCompileWithErrorLogging
LogStringAndSeparatorP ( errNameStr, '\t' );
HLock ( (Handle)errStrHdl );
LogStringBreakP ( *errStrHdl );
HUnlock ( (Handle)errStrHdl );
#endif
errorSystemDisposeString ( errStrHdl );
}
else
{
/* can't get system error string */
#if kCompileWithErrorLogging
LogStringBreakP ( "\p(can't load error message string)" );
#endif /* kCompileWithErrorLogging */
#endif
SysBeep ( 5 );
#if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
}
#endif
} /* ErrorSystem */
#pragma segment Startup
/* A 'fatal' startup error has occurred. This usually means that a required
system software component is missing (IE. Apple Events) and/or the
System version is too old. */
p_export
void
ErrorStartup ( short errNum )
{
#if kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface)
Str255 errStr; /* the retrieved error string */
#if kCompileWithErrorLogging
Str255 errNumStr; /* used for string version of the error number */
/* Create a pascal format string of the error number. */
NumToString ( errNum, errNumStr );
LogStringAndSeparatorP ( errNumStr, '\t' );
#endif /* kCompileWithErrorLogging */
errorStartupGetString ( errNum, &errStr );
#if kCompileWithForeground && kCompileWithFullUserInterface
/* Display the alert. */
ErrorAlertStartupString ( (OSErr)errNum, &errStr );
#endif
#if kCompileWithErrorLogging
LogStringBreakP ( errStr );
LogQuit ();
#endif /* kCompileWithErrorLogging */
#endif /* kCompileWithErrorLogging || (kCompileWithForeground && kCompileWithFullUserInterface) */
ExitToShell ();
} /* ErrorStartup */
#pragma segment Main
/** String Retreival **/
/* return the system error's human readable string and it's constant name (errNameStr) */
static StringHandle
errorSystemGetString ( OSErr errNum, Str255 *errNameStr )
{
StringHandle errStrHdl; /* the retrieved error string */
StringHandle theString; /* the string to return */
short errID; /* just used for GetResInfo call */
ResType errType; /* just used for GetResInfo call */
/* Get message string for the error number. */
errStrHdl = (StringHandle) GetResource ( (ResType)krtErrStr, (short)errNum );
theString = NULL;
if ( errStrHdl != NULL )
{
theString = (StringHandle) MemoryNewHandle ( sizeof(Str255), NULL );
}
if ( theString == NULL )
{
/* if string isn't available, use generic error string */
theString = gSystemErrorStr;
(*errNameStr)[0] = nil;
}
else
{
my_assert ( (HGetState((Handle)errStrHdl) & kMemoryHandleLockedFlag) == nil,
"\perrorSystemGetString: errStrHdl is already locked!" );
my_assert ( (HGetState((Handle)theString) & kMemoryHandleLockedFlag) == nil,
"\perrorSystemGetString: theString is already locked!" );
HLock ( (Handle)errStrHdl );
HLock ( (Handle)theString );
StringPascalCopy ( (char *)(*errStrHdl), (char *)(*theString) );
HUnlock ( (Handle)errStrHdl );
HUnlock ( (Handle)theString );
ReleaseResource ( (Handle)errStrHdl );
/* get the error name from the resource */
GetResInfo ( (Handle)errStrHdl, &errID, &errType, *errNameStr );
}
return theString;
} /* errorSystemGetString */
/* dispose of the error string handle - don't do anything if it is the default string */
static void
errorSystemDisposeString ( StringHandle theString )
{
my_assert ( theString != NULL, "\perrorSystemDisposeString: theString is NULL" );
if ( theString != gSystemErrorStr )
{
DisposeHandle ( (Handle)theString );
}
} /* errorSystemDisposeString */
#pragma segment Startup
/* */
static void
errorStartupGetString ( short errNum, Str255 *errStr )
{
/* Get message string for the error number. */
GetIndString ( *errStr, krStartupErrorStrs, errNum );
if ( *errStr == nil )
{
/* if string isn't available, use generic error string */
StringPascalCopy ( (char *)kstrErrorMissing, (char *)(*errStr) );
}
} /* errorStartupGetString */
#pragma segment Main
/***** EOF *****/